﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormStore.BackManager.SocketTool;
using WinFormStore.Basic;
using WinFormStore.DataSelect;
using WinFormStore.Erp;
using WinFormStore.ScannerSerialPro;
using WinFormStore.Store;
using WinFormStore.实体库;
using WinFormStore.数据层;
using WinFormStore.通信层;


namespace WinFormStore
{
    public partial class FrmMain : Form
    {
        //配置文件路径
        public string path;
        //配置服务对象
        public ScannerInfoService infoService ;
        //配置实体对象
        public ScannerInfo scannerInfo ;

        public ScannerComService comService;//声明数据层对象

        public FrmMain(string _path, ScannerInfoService _infoService, ScannerInfo _scannerInfo)
        {
           // 
            InitializeComponent();
            path = _path;
            infoService = _infoService;
            scannerInfo = _scannerInfo;
        }

        public FrmMain()
        {
            InitializeComponent();
            timer1.Tick += new EventHandler(timer1_Tick);
        }

        //将窗体显示到panel里
        public void CloseFrom()
        {
            foreach (Control item in this.panel1.Controls)
            {
                if (item is Form)
                {
                    Form form = (Form)item;

                    ((Form)item).Close();
                    ((Form)item).Dispose();
                }
            }
        }
        public void ShowFrom(Form f)
        {
            CloseFrom();
            //清除panel里面的其他窗体
            //this.panel1.Controls.Clear();
            //将该子窗体设置成非顶级控件
            f.TopLevel = false;
            //将该子窗体的边框去掉
            f.FormBorderStyle = FormBorderStyle.None;
            //设置子窗体随容器大小自动调整
            f.Dock = DockStyle.Fill;
            //设置mdi父容器为当前窗口
            f.Parent = this.panel1;
            //子窗体显示
            f.Show();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            panel5.Parent=this.panel7 ;
            panel6.Parent=this.panel7 ;
           
            this.lblWelcome.Text = LoginConfig.SalesManName + "（" + LoginConfig.Role + "）" + "欢迎您";
            this.lblTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
            FrmAd frmAd = new FrmAd();
            ShowFrom(frmAd);
            if (LoginConfig.Role != "店长")
            {
                panel5.Visible = false;
            }
            else
            {
                panel6.Visible = false;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //DateTime current = DateTime.Now;
            //DateTime startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
            //DateTime endTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0);
            //this.lblTime.Text = current.ToString("yyyy年MM月dd日 HH:mm:ss");
            //if (startTime >= current || current >= endTime)
            //{

            //    btn充值.Enabled = false;
            //    btn商店商品.Enabled = false;
            //    //timer1.Stop();
            //    //MessageBox.Show("商店功能已关闭，你可以查看订单状态");
            //}
        }
        private void CloseApp()
        {
            CloseFrom();
            this.Close();
            this.Dispose();
            FrmLogin frmLogin = new FrmLogin();
            frmLogin.ShowDialog();
        }
        private void button_click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (btn.Name != "btnExit")
            {
                if (btn.Name == "btn商品分类")
                {
                    FrmType frmType = new FrmType();
                    ShowFrom(frmType);
                }
                if (btn.Name == "btn员工管理")
                {
                    FrmSaleMan frmSaleMan = new FrmSaleMan();
                    ShowFrom(frmSaleMan);
                }
                if (btn.Name == "btn销售统计")
                {
                    FrmSales frmSales = new FrmSales();
                    ShowFrom(frmSales);
                }
                if (btn.Name == "btn工资核算")
                {
                    FrmSalary frmSalary = new FrmSalary();
                    ShowFrom(frmSalary);
                }
                if (btn.Name == "btn首页")
                {
                    FrmHome frmHome = new FrmHome();
                    ShowFrom(frmHome);
                }
                if (btn.Name == "btn充值")
                {
                    FrmRecharge frmRecharge = new FrmRecharge();
                    ShowFrom(frmRecharge);
                }
                if (btn.Name == "btn商店商品")
                {
                    FrmStore frmStore = new FrmStore();
                    ShowFrom(frmStore);
                }
                if (btn.Name == "btn购买记录")
                {
                    FrmPurchaseHistory frmPurchaseHistory = new FrmPurchaseHistory();
                    ShowFrom(frmPurchaseHistory);
                }
                if (btn.Name == "btn定位商店")
                {
                    FrmLocation frmLocation = new FrmLocation();
                    ShowFrom(frmLocation);
                }
                if (btn.Name == "btn设置")
                {
                    FrmSet frmSet = new FrmSet();
                    ShowFrom(frmSet);
                }
            }
            else
            {
                CloseApp();
            }
        }
        private void Btn商品浏览_Click(object sender, EventArgs e)
        {
            FrmGoodView frmGoodView = new FrmGoodView(path, infoService, scannerInfo);
            ShowFrom(frmGoodView);
        }

        private void Btn商品入库_Click(object sender, EventArgs e)
        {
            FrmGoodPutIn frmGoodPutIn = new FrmGoodPutIn(path, infoService, scannerInfo);
            ShowFrom(frmGoodPutIn);
        }

        public void Btn收银台_Click(object sender, EventArgs e)
        {
            FrmCash frmCash = new FrmCash(path, infoService, scannerInfo);
            ShowFrom(frmCash);
        }

        private void label1_Click(object sender, EventArgs e)
        {
            FrmAd frmAd = new FrmAd();
            ShowFrom(frmAd);
        }

        private void btn_shujvtubiao_Click(object sender, EventArgs e)
        {
            FrmChart frmChart = new FrmChart();
            ShowFrom(frmChart);
        }

        private void btnSocket_Click(object sender, EventArgs e)
        {
            FrmSocket frmSocket = new FrmSocket();
            ShowFrom(frmSocket);
        }
        private Point panelPoint;
        private void panel4_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - panelPoint.X, this.Location.Y + e.Y - panelPoint.Y);
            }
        }

        private void panel4_MouseDown(object sender, MouseEventArgs e)
        {
            panelPoint = e.Location;
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
           
        }
    }
}
